package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.entity.BillLading;
import com.macro.mall.service.BillLadingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/lading")
@Api(tags = "BillLadingController", description = "提单管理")
public class BillLadingController {

    @Autowired
    private BillLadingService billLadingService;

    @ApiOperation("添加提单记录")
    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    public CommonResult addOne(@RequestBody BillLading billLading) {
        try {
            billLadingService.saveNotNull(billLading);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(null, "添加成功");
    }

    @ApiOperation("删除提单记录")
    @RequestMapping(value = "/delOne", method = RequestMethod.POST)
    public CommonResult delOne(Integer id) {
        try {
            billLadingService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(null, "删除成功");
    }

    @ApiOperation("分页查询提单记录")
    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    public CommonResult queryPage(String iphone, Integer page, Integer size) {
        List<BillLading> filesIPage = null;
        try {
            filesIPage = billLadingService.find(iphone, page, size);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(filesIPage);
    }

    @ApiOperation("修改提单记录")
    @RequestMapping(value = "/updateOne", method = RequestMethod.POST)
    public CommonResult updateOne(@RequestBody BillLading billLading) {
        try {
            billLadingService.updateNotNullById(billLading);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(null, "修改成功");
    }
}
